package cn.iocoder.an.module.mold.controller.admin.drawingnobom;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.drawingnobom.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.drawingnobom.DrawingNoBomDO;
import cn.iocoder.an.module.mold.service.drawingnobom.DrawingNoBomService;

@Tag(name = "管理后台 - 模具图号BOM")
@RestController
@RequestMapping("/mold/drawing-no-bom")
@Validated
public class DrawingNoBomController {

    @Resource
    private DrawingNoBomService drawingNoBomService;

    @PostMapping("/create")
    @Operation(summary = "创建模具图号BOM")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no-bom:create')")
    public CommonResult<Long> createDrawingNoBom(@Valid @RequestBody DrawingNoBomSaveReqVO createReqVO) {
        return success(drawingNoBomService.createDrawingNoBom(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具图号BOM")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no-bom:update')")
    public CommonResult<Boolean> updateDrawingNoBom(@Valid @RequestBody DrawingNoBomSaveReqVO updateReqVO) {
        drawingNoBomService.updateDrawingNoBom(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具图号BOM")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:drawing-no-bom:delete')")
    public CommonResult<Boolean> deleteDrawingNoBom(@RequestParam("id") Long id) {
        drawingNoBomService.deleteDrawingNoBom(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得模具图号BOM")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no-bom:query')")
    public CommonResult<DrawingNoBomRespVO> getDrawingNoBom(@RequestParam("id") Long id) {
        DrawingNoBomDO drawingNoBom = drawingNoBomService.getDrawingNoBom(id);
        return success(BeanUtils.toBean(drawingNoBom, DrawingNoBomRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得模具图号BOM列表")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no-bom:query')")
    public CommonResult<List<DrawingNoBomRespVO>> getDrawingNoBomList(@Valid DrawingNoBomListReqVO listReqVO) {
        List<DrawingNoBomDO> list = drawingNoBomService.getDrawingNoBomList(listReqVO);
        return success(BeanUtils.toBean(list, DrawingNoBomRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具图号BOM Excel")
    @PreAuthorize("@ss.hasPermission('mold:drawing-no-bom:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDrawingNoBomExcel(@Valid DrawingNoBomListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<DrawingNoBomDO> list = drawingNoBomService.getDrawingNoBomList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "模具图号BOM.xls", "数据", DrawingNoBomRespVO.class,
                        BeanUtils.toBean(list, DrawingNoBomRespVO.class));
    }

}